package com.quick.threadtest;

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {
    public static void main(String[] args) throws Exception{
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, ()-> {
                System.out.println("Action ... go!!");
        });
        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(new CyclicBarrierorker(cyclicBarrier));
            t.start();
        }
    }
}
class CyclicBarrierorker implements Runnable{
    private CyclicBarrier cyclicBarrier;
    public CyclicBarrierorker(CyclicBarrier cyclicBarrier){
        this.cyclicBarrier = cyclicBarrier;
    }
    @Override
    public void run() {
        try {
            for (int i = 0; i < 3; i++) {
                System.out.println("Executed");
                cyclicBarrier.await();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
